package com.newland.mesdk.demo.device;

import com.newland.mtypex.audioport.AudioPortV100ConnParams;
import com.newland.mesdk.demo.activity.BaseActivity;
import com.newland.mesdk.demo.moduleinterface.DeviceControllerInterface;
import com.newland.mesdk.demo.util.Const.MessageTag;



/**
 * Created by YJF on 2015/9/7 0007. 音频设备
 */
public class AudioDevice extends AbstractDevice {
	private static final String ME3X_DRIVER_NAME = "com.newland.me.ME3xDriver";
	private BaseActivity baseActivity;
	private DeviceControllerInterface controller = null;

	public AudioDevice(BaseActivity baseActivity) {
		super();
		this.baseActivity = baseActivity;
	}

	// 初始化音频设备控制器
	@Override
	public void initController() {
		ME3xDeviceDriver me3xDeviceController = new ME3xDeviceDriver(baseActivity);
		controller = me3xDeviceController.initMe3xDeviceController(ME3X_DRIVER_NAME, new AudioPortV100ConnParams());
		baseActivity.showMessage("Audio控制器已初始化!", MessageTag.TIP);
		baseActivity.btnStateToWaitingConn();
	}

	@Override
	public void disconnect() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					if (controller != null) {
						controller.disConnect();
						controller = null;
						baseActivity.showMessage("Audio控制器断开成功.", MessageTag.TIP);
						baseActivity.btnStateToWaitingInit();
					}
				} catch (Exception e) {
					baseActivity.showMessage("Audio控制器断开异常:" + e, MessageTag.ERROR);
				}
			}
		}).start();

	}

	@Override
	public boolean isControllerAlive() {
		if (controller != null) {
			return true;
		}
		return false;
	}

	@Override
	public DeviceControllerInterface getController() {
		return controller;
	}

	@Override
	public void connectDevice() {
		baseActivity.showMessage("Audio设备连接中...", MessageTag.TIP);
		try {
			controller.connect();
			baseActivity.showMessage("Audio设备连接成功...", MessageTag.TIP);
		} catch (Exception e1) {
			e1.printStackTrace();
			baseActivity.showMessage("音频链接异常,请检查设备或重新连接..." + e1, MessageTag.ERROR);
		}

	}
}
